package com.sky.controller.admin;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> selectPage(CategoryPageQueryDTO categoryPageQueryDTO){
        System.out.println(categoryPageQueryDTO);
        log.info("categoryPageQueryDTO + {}",categoryPageQueryDTO);
        PageResult page = categoryService.selectPage(categoryPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 根据Id删除分页
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteByIdCategory(Integer id){
        try {
            categoryService.deleteByIdCategory(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }


    /**
     * 启动或禁用功能
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result StartupDisable(@PathVariable Integer status, Integer id) {
        categoryService.StartupDisable(status,id);
        return Result.success();
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO category){
        categoryService.addCategory(category);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryId
     * @return
     */
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryId){
        categoryService.updateCategory(categoryId);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}

